package com.jovision.sovplay;

import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.base.BaseActivity;
import com.jovision.JVNetConst;
import com.jovision.JniUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class JVRemotePlayBackActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "JVRemotePlayBackActivity";

    //intent 参数
    private int window;
    private String acBuffStr;

    //views
    private SurfaceView playSurface;// 视频播放view
    private SurfaceHolder holder;
    private TextView linkStateTV;// 连接状态
    private SeekBar progressBar;
    private ImageButton pauseImgBtn;

    //variable
    private int currentProgress = 0;// 当前进度
    private int seekProgress;// 手动进度
    /**
     * 远程回放进度条拖动事件
     */
    OnSeekBarChangeListener mOnSeekBarChangeListener = new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
            seekProgress = arg1;
        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
            currentProgress = seekProgress;
            SovUtil.streamSeekTo(window, seekProgress);
        }
    };
    private int totalProgress;// 总进度
    private Boolean isPaused = false;// 是否已暂停

    @Override
    public void onHandler(int what, int arg1, int arg2, Object obj) {

        Log.e(TAG,"onHandler,what"+what+";arg1="+arg1+";arg2="+arg2+";obj="+obj);
        switch (what) {
            //连接结果,视频异常断开时关闭此界面
            case JVNetConst.CALL_CATEYE_CONNECTED: {
                if (arg2 != JVNetConst.CCONNECTTYPE_CONNOK) {
                    this.finish();
                }
                break;
            }
            //出图
            case JVNetConst.CALL_NEW_PICTURE: {
                linkStateTV.setVisibility(View.GONE);
                break;
            }
            // 远程回放数据
            case JVNetConst.CALL_PLAY_DATA: {
                switch (arg2) {
                    case JVNetConst.JVN_DATA_O: {
                        if (0 == totalProgress) {
                            try {
                                JSONObject jobj;
                                jobj = new JSONObject(obj.toString());
                                if (null != jobj) {
                                    totalProgress = jobj.optInt("total");
                                    progressBar.setMax(totalProgress);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        break;
                    }
                    case JVNetConst.JVN_DATA_I:
                    case JVNetConst.JVN_DATA_B:
                    case JVNetConst.JVN_DATA_P: {
                        currentProgress++;
                        progressBar.setProgress(currentProgress);
                        break;
                    }
                    //远程回放结束回调
                    case JVNetConst.REMOTE_PLAY_CMD_OVER: {
                        Toast.makeText(JVRemotePlayBackActivity.this,"远程回放结束",Toast.LENGTH_LONG).show();
                        backMethod();
                        break;
                    }
                    //远程回放出错
                    case JVNetConst.REMOTE_PLAY_CMD_PLAYERROR: {
                        Toast.makeText(JVRemotePlayBackActivity.this,"远程回放出错",Toast.LENGTH_LONG).show();
                        backMethod();
                        break;
                    }
                    //远程回放超时
                    case JVNetConst.REMOTE_PLAY_CMD_PLTIMEOUT: {
                        Toast.makeText(JVRemotePlayBackActivity.this,"远程回放超时",Toast.LENGTH_LONG).show();
                        backMethod();
                        break;
                    }

                }

                break;
            }

            default:
                break;
        }

    }

    @Override
    public void onNotify(int what, int arg1, int arg2, Object obj) {
        handler.sendMessage(handler.obtainMessage(what, arg1, arg2, obj));
    }

    @Override
    protected void initSettings() {
        Intent intent = getIntent();
        if (null != intent) {
            window = intent.getIntExtra("window", 0);
            acBuffStr = intent.getStringExtra("acBuffStr");
        }
    }

    @Override
    protected void initUi() {
        setContentView(R.layout.activity_playback);
        ((TextView) findViewById(R.id.title_tv)).setText("视频回放");
        playSurface = (SurfaceView) findViewById(R.id.playsurface);
        linkStateTV = (TextView) findViewById(R.id.linkstate);
        linkStateTV.setTextColor(Color.GREEN);
        linkStateTV.setVisibility(View.VISIBLE);
        linkStateTV.setText("连接中......");

        progressBar = (SeekBar) findViewById(R.id.playback_seekback);
        progressBar.setOnSeekBarChangeListener(mOnSeekBarChangeListener);
        pauseImgBtn = (ImageButton) findViewById(R.id.playbackpause);
        pauseImgBtn.setOnClickListener(this);

        holder = playSurface.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                JniUtil.pauseSurface(window);
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {

                boolean enable = SovUtil.enableRemotePlay(window, true);
                if (enable) {
                    SovUtil.startStreamRemotePlay(window, acBuffStr);
                }
                JniUtil.resumeSurface(window,
                        holder.getSurface());
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,
                                       int width, int height) {
            }
        });
        findViewById(R.id.ll_title_left_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        backMethod();
    }

    public void backMethod(){
        SovUtil.stopStreamRemotePlay(window);
        SovUtil.enableRemotePlay(window,false);
        JVRemotePlayBackActivity.this.finish();
    }
    @Override
    protected void saveSettings() {

    }

    @Override
    protected void freeMe() {
        SovUtil.enableRemotePlay(window, false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.playbackpause: {// 暂停继续按钮
                if (isPaused) {
                    isPaused = false;
                    pauseImgBtn.setImageResource(R.drawable.video_stop_icon);
                    // 继续播放视频
                    SovUtil.goonStreamCatRemotePlay(window);
                } else {
                    isPaused = true;
                    // 暂停视频
                    SovUtil.pauseStreamCatRemotePlay(window);
                    pauseImgBtn.setImageResource(R.drawable.video_play_icon);
                }
                break;
            }
        }
    }
}
